import React from 'react';
import { Card } from 'antd';
import { PageContainer } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import Search from './components/Search';
import { summaryPageList } from '@/services/Collection';
import Export from './components/Export';
import columns from './columns';

class Summary extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      params: {
        type: 0
      }
    }
    this.proTableRef = React.createRef();
  }

  /* 获取注销列表 */
  getPageList = async (currentParams) => {
    try {
      const params = this.state.params;   
      const res = await summaryPageList({
        ...params,
        ...currentParams
      })
      return {
        data: res.data.map(function (item, index) {
          item.index = index + '-' + new Date().getTime();
          return item
        }),
        total: res.totalSize,
        success: true
      }
    } catch (e) {
      console.error(e)
    }
  }

  /* 监听查询事件 */
  onSearch = (data) => {
    const params = this.state.params;
    this.setState({ params: { ...params, ...data } });
    this.proTableRef.current.reload();
  }

  /* 刷新事件 */
  onReload = () => {
    const type = this.state.params.type;
    this.setState({ params: { type } });
    this.proTableRef.current.reload();
  }

  /* 获取radio变化 */
  onRadioChange = (type) => {
    const params = this.state.params;
    params.type = type;
    this.setState({params});
    this.proTableRef.current.reload();
  }

  render() {
    const { onSearch, onReload, onRadioChange} = this;
    const proTableRef = this.proTableRef;
    const params = this.state.params;
    return (
      <PageContainer>
        <Card
          title='总括列表'
          bodyStyle={{ paddingBottom: 0, paddingTop: 30 }}
          bordered={false}>
          <Search search={onSearch} reset={onReload} />
        </Card>
        <ProTable
          actionRef={proTableRef}
          rowKey='index'
          headerTitle={
            <Export query={params} change={onRadioChange}></Export>
          }
          search={false}
          columns={columns}
          request={this.getPageList} />
      </PageContainer>
    );
  }
}

export default Summary;
